package tw.com.diray.comm.ui;

import java.util.Calendar;

import tw.com.diary.ae.com.DirayBussinessException;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.text.TextUtils;
import android.widget.DatePicker;

public class DirDatePickerDialog {

	private DatePickerDialog dialog;
	private DirDateBean dirDateBean;
	private Context context;
	private DatePickerListenter listenter;

	public void create(Context context, final String yyyymmdd,
			DatePickerListenter listener) throws DirayBussinessException {
		this.context = context;
		this.listenter = listener;

		this.dirDateBean = new DirDateBean();

		final Calendar c = Calendar.getInstance();

		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH);
		int day = c.get(Calendar.DAY_OF_MONTH);

		if (TextUtils.isEmpty(yyyymmdd)) {// 日期是空值

			year = c.get(Calendar.YEAR);
			month = c.get(Calendar.MONTH);
			day = c.get(Calendar.DAY_OF_MONTH);

		} else {

			// 有值

			year = Integer.parseInt(yyyymmdd.substring(0, 3));
			month = Integer.parseInt(yyyymmdd.substring(3, 5)) - 1;
			day = Integer.parseInt(yyyymmdd.substring(5));

		}
		this.dialog = new DatePickerDialog(context, new OnDateSetListener() {

			@Override
			public void onDateSet(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				dirDateBean.setDay(dayOfMonth);
				dirDateBean.setMonth(monthOfYear);
				dirDateBean.setYear(year);

				listenter.callback(dirDateBean);

			}
		}, year, month, day);

	}

	public void show() {
		this.dialog.show();
	}

	public void dismiss() {
		this.dialog.dismiss();
	}

}
